/**
 * @file client.c 客户端主函数
 *       用于与服务器进行通信
 * @author pzk (1528089905@qq.com)
 * @brief 
 * @version 1.5
 * @date 2025-8-25
 * 
 * @copyright copyright (c) 2025
 */
#include "client.h"

int client_sock;                  // 客户端套接字
int current_user_id = -1;         // 当前用户ID  
char current_username[32] = {0};  // 当前用户名
char server_ip[16] = {0};         // 服务器IP地址
int server_port = 0;              // 服务器端口号

int main(int argc, char *argv[])
{
    struct sockaddr_in server_addr;
    int ret;
    // 检查命令行参数
    SYSERR2(argc, !=, 3, "Usage: ./cli <server IP> <port>", exit(1));

    // 保存服务器地址和端口
    strncpy(server_ip, argv[1], sizeof(server_ip)-1);
    server_port = atoi(argv[2]);

    // 创建TCP套接字
    client_sock = socket(AF_INET, SOCK_STREAM, 0);
    SYSERR(client_sock,<, 0, "socket failed", -1);

    // 设置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);

    // 将IP地址从字符串转换为网络地址
    ret = inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
    SYSERR2(ret, <=, 0, "Invalid IP address", exit(1));

     // 连接到服务器
    ret = connect(client_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
    SYSERR2(ret, <, 0, "connect failed", exit(1));

    printf("[Client] Connect to server successfully\n");
    sleep(1);
    system("clear");
    Login_main_menu();

    close(client_sock);
    return 0;
}
